package cn.edu.xhu.library_management.area.controller;

import cn.edu.xhu.library_management.area.entity.LmItemNum;
import cn.edu.xhu.library_management.area.service.ItemNumService;
import cn.edu.xhu.library_management.common.entity.ItemType;
import cn.edu.xhu.library_management.common.entity.R;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.apiguardian.api.API;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: MMJ
 * @Date: 2022 - 11 - 18
 * @Description: 物品数量Controller
 */
@RestController
@Api(tags = "物品数量模块")
@ApiSort(4)
@RequestMapping("/lm_area")
public class ItemNumController {
    @Autowired
    private ItemNumService itemNumService;

    @GetMapping("/itemNum")
    @ApiOperation(value = "通过区域Id,获取物品数量")
    @ApiOperationSupport(order = 411)
    public R findItemNumsByAreaId(@Param("areaId") Integer areaId){
        LmItemNum nums = itemNumService.findItemNumsByAreaId(areaId);
        return nums != null ? R.ok().data("nums",nums):R.error("查无此数据!!!");
    }
    @GetMapping("/itemType")
    @ApiOperation(value = "获取物品类型")
    @ApiOperationSupport(order = 412)
    public R findItemType(){
        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> map = null;
        for (ItemType t: ItemType.values()){
            map = new HashMap<>();
            map.put("key",t.getCode()+"");
            map.put("value",t.getName());

            list.add(map);
        }

        return R.ok().data("types",list);
    }



    @PostMapping("/itemNum")
    @ApiOperation(value = "添加物品信息")
    @ApiOperationSupport(order = 421)
    public R addItemNum(@RequestBody LmItemNum itemNum){
        return itemNumService.addItemNum(itemNum)?R.ok("添加物品数量成功!!!"):R.error("添加物品数量失败!!!");
    }


    @PutMapping("/itemNum")
    @ApiOperation(value = "通过物品ID，修改物品数量信息")
    @ApiOperationSupport(order = 431)
    public R updateItemNum(@RequestBody LmItemNum itemNum){
        return itemNumService.updateItemNum(itemNum)?R.ok("修改物品数量成功!!!"):R.error("修改物品数量失败!!!");
    }


    @DeleteMapping("/itemNum")
    @ApiOperation(value = "通过物品Id，删除物品数量信息")
    @ApiOperationSupport(order = 441)
    public R deleteItemNumById(Integer id){
        return itemNumService.deleteItemNumById(id)?R.ok("删除物品数量成功!!!"):R.error("删除物品数量失败!!!");
    }
}
